{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Partition Array Into Three Parts With Equal Sum"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #greedy #array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #贪心 #数组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: canThreePartsEqualSum"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #将数组分成和相等的三个部分"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个整数数组 <code>arr</code>，只有可以将其划分为三个和相等的 <strong>非空</strong> 部分时才返回 <code>true</code>，否则返回 <code>false</code>。</p>\n",
    "\n",
    "<p>形式上，如果可以找出索引 <code>i + 1 < j</code> 且满足 <code>(arr[0] + arr[1] + ... + arr[i] == arr[i + 1] + arr[i + 2] + ... + arr[j - 1] == arr[j] + arr[j + 1] + ... + arr[arr.length - 1])</code> 就可以将数组三等分。</p>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>arr = [0,2,1,-6,6,-7,9,1,2,0,1]\n",
    "<strong>输出：</strong>true\n",
    "<strong>解释：</strong>0 + 2 + 1 = -6 + 6 - 7 + 9 + 1 = 2 + 0 + 1\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>arr = [0,2,1,-6,6,7,9,-1,2,0,1]\n",
    "<strong>输出：</strong>false\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>arr = [3,3,6,5,-2,2,5,1,-9,4]\n",
    "<strong>输出：</strong>true\n",
    "<strong>解释：</strong>3 + 3 = 6 = 5 - 2 + 2 + 5 + 1 - 9 + 4\n",
    "</pre>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>3 <= arr.length <= 5 * 10<sup>4</sup></code></li>\n",
    "\t<li><code>-10<sup>4</sup> <= arr[i] <= 10<sup>4</sup></code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [partition-array-into-three-parts-with-equal-sum](https://leetcode.cn/problems/partition-array-into-three-parts-with-equal-sum/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [partition-array-into-three-parts-with-equal-sum](https://leetcode.cn/problems/partition-array-into-three-parts-with-equal-sum/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[0,2,1,-6,6,-7,9,1,2,0,1]', '[0,2,1,-6,6,7,9,-1,2,0,1]', '[3,3,6,5,-2,2,5,1,-9,4]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        temp=0\n",
    "        flag=False\n",
    "        res_s=sum(arr)\n",
    "        s=0\n",
    "        for x in range(len(arr)):\n",
    "            s+=arr[x]\n",
    "            if not flag and 3*s==res_s:\n",
    "                temp=s\n",
    "                flag=True\n",
    "                continue\n",
    "            elif flag and s==2*temp and x<len(arr)-1:\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        partition_sum = sum(arr) / 3\n",
    "        cur_p_sum = 0\n",
    "        flags = []\n",
    "        for each_num in arr:\n",
    "            cur_p_sum += each_num\n",
    "            if cur_p_sum == partition_sum:\n",
    "                cur_p_sum = 0\n",
    "                flags.append(True)\n",
    "        return len(flags) >= 3\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        sums = sum(arr)\n",
    "        if sums % 3 != 0:\n",
    "            return False\n",
    "        target = sums // 3\n",
    "        nums = 0\n",
    "        tmp_sum = 0\n",
    "        for val in arr:\n",
    "            tmp_sum += val\n",
    "            if tmp_sum == target:\n",
    "                nums += 1\n",
    "                tmp_sum = 0\n",
    "            else:\n",
    "                continue\n",
    "        return nums >= 3\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        s=sum(arr)\n",
    "        if s%3:return False\n",
    "        a=s//3\n",
    "        p=False\n",
    "        k=0\n",
    "        for i in range(len(arr)):\n",
    "            k+=arr[i]\n",
    "            if k==a and not p:\n",
    "                p=True\n",
    "                continue\n",
    "            if k==2*a and i!=len(arr)-1 and p:\n",
    "                return True\n",
    "        return False\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        if sum(arr) % 3 != 0:\n",
    "            return False\n",
    "        divide_sum = sum(arr) // 3\n",
    "        i = 0\n",
    "        cnt = 3\n",
    "        single_sum = 0\n",
    "        while i < len(arr):\n",
    "            if single_sum + arr[i] == divide_sum:\n",
    "                if cnt > 0:\n",
    "                    cnt -= 1\n",
    "                    single_sum = 0\n",
    "                else:\n",
    "                    single_sum += arr[i]\n",
    "            else:\n",
    "                single_sum += arr[i]\n",
    "            i += 1\n",
    "        return cnt == 0 and single_sum == 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        if sum(arr)%3 != 0: return False\n",
    "        time = 0\n",
    "        value = 0\n",
    "        for i in arr:\n",
    "            value += i\n",
    "            if value == sum(arr)//3:\n",
    "                time += 1\n",
    "                value = 0\n",
    "            if time == 3:return True\n",
    "        return False\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        # 对数组求和\n",
    "        arrSum = sum(arr)\n",
    "        # 如果数组和不能被 3 整除，则不能划分为三个和相等的\n",
    "        if arrSum % 3 != 0:\n",
    "            return False\n",
    "        # 求“相等的和”的“和”\n",
    "        singleSum = arrSum // 3\n",
    "        # 三部分\n",
    "        cnt = 3\n",
    "        # 记录每部分的和\n",
    "        tempSum = 0\n",
    "\n",
    "        for i in arr:\n",
    "            if tempSum + i == singleSum:\n",
    "                if cnt > 0:\n",
    "                    cnt -= 1\n",
    "                    tempSum = 0\n",
    "                else:\n",
    "                    tempSum += i\n",
    "            else:\n",
    "                tempSum += i\n",
    "\n",
    "        if cnt == 0 and tempSum == 0:\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        lenth = len(arr)\n",
    "        sum = 0\n",
    "        for a in arr:\n",
    "            sum +=a\n",
    "        if sum%3 !=0:\n",
    "            return False\n",
    "        target = sum//3\n",
    "        count=0\n",
    "        sum = 0\n",
    "        for i in range(lenth):\n",
    "            sum += arr[i]\n",
    "            if sum == target:\n",
    "                count+=1\n",
    "                sum = 0\n",
    "        if count >=3:\n",
    "            return True\n",
    "        else :\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# https://leetcode.cn/problems/partition-array-into-three-parts-with-equal-sum/\n",
    "from typing import List\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        if arr.__len__() < 3:\n",
    "            return False\n",
    "        sm = sum(arr)\n",
    "        # print(\"sm=\", sm)\n",
    "        if sm % 3 != 0:\n",
    "            return False\n",
    "        counter = 0\n",
    "        tmp = 0\n",
    "        for num in arr:\n",
    "            tmp += num\n",
    "            if tmp == sm/3:\n",
    "                counter += 1\n",
    "                tmp = 0\n",
    "        return counter >= 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        sumV = sum(arr)\n",
    "        if sumV % 3 != 0 :\n",
    "            return False\n",
    "\n",
    "        partSum = times = 0\n",
    "        for num in arr :\n",
    "            partSum += num \n",
    "            if partSum == sumV // 3 :\n",
    "                partSum = 0 \n",
    "                times += 1\n",
    "        \n",
    "        return times >= 3\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        # if arr.count(0)==len(arr):\n",
    "        #     return True\n",
    "        s=sum(arr)\n",
    "        if s%3!=0:\n",
    "            return False\n",
    "        s//=3\n",
    "        i=-1\n",
    "        j=-1\n",
    "        h=0\n",
    "        for a in range(len(arr)):\n",
    "            h+=arr[a]\n",
    "            if i<0 and h==s:\n",
    "                i=a\n",
    "            elif i>=0 and j<0 and h==s*2:\n",
    "                j=a\n",
    "        if i+1<=j and j<len(arr)-1:\n",
    "            return True\n",
    "        else:\n",
    "            return False\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        n, s = len(arr), sum(arr)\n",
    "        if s%3:\n",
    "            return False\n",
    "        tar = s // 3\n",
    "        tmp = 0\n",
    "        time = 0\n",
    "        for num in arr:\n",
    "            tmp += num\n",
    "            if tmp == tar:\n",
    "                tmp = 0\n",
    "                time += 1\n",
    "        if time >= 3:\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        l = len(arr)\n",
    "        arr_sum = sum(arr)\n",
    "        if (arr_sum%3) != 0:\n",
    "            return False\n",
    "        arr_sum = arr_sum/3\n",
    "        num = count = 0\n",
    "        for i in range(l):\n",
    "            num += arr[i]\n",
    "            if num == arr_sum:\n",
    "                count += 1\n",
    "                num = 0\n",
    "                if count < 3:\n",
    "                    continue\n",
    "            if count == 3:\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        new_arr=[0]*(len(arr))\n",
    "        s=0\n",
    "        for x in range(len(arr)):\n",
    "            s+=arr[x]\n",
    "            new_arr[x]=s\n",
    "        res_s=-1\n",
    "        start=0\n",
    "        while start<len(arr):\n",
    "            if s==3*new_arr[start]:\n",
    "                res_s=start\n",
    "                break\n",
    "            start+=1\n",
    "        else:\n",
    "            return False\n",
    "        mid = start+1\n",
    "        res_mid=-1\n",
    "        while mid<len(arr):\n",
    "            if new_arr[mid] == 2*new_arr[start]:\n",
    "                res_mid=mid\n",
    "                break\n",
    "            mid+=1\n",
    "        else:\n",
    "            return False\n",
    "        if res_mid<len(arr)-1:\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        arrsum = sum(arr)\n",
    "        if arrsum % 3 != 0:\n",
    "            return False\n",
    "        singlesum = arrsum // 3\n",
    "        count = 3\n",
    "        temp = 0\n",
    "\n",
    "        for i in arr:\n",
    "            if temp + i == singlesum:\n",
    "                if count > 0:\n",
    "                    count -= 1\n",
    "                    temp = 0\n",
    "                else:\n",
    "                    temp += i\n",
    "            else:\n",
    "                temp += i\n",
    "\n",
    "        if count == 0 and temp == 0:\n",
    "            return True \n",
    "        else:\n",
    "            return False  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        sum_arr = sum(arr)\n",
    "        len_arr = len(arr)\n",
    "        print(sum_arr)\n",
    "        if sum_arr % 3 != 0:\n",
    "            return False\n",
    "        \n",
    "        i,sum1 = 0, 0\n",
    "        while i < len_arr - 1:\n",
    "            sum1 += arr[i]\n",
    "            i += 1\n",
    "            if sum1 == sum_arr // 3 :\n",
    "                break\n",
    "        j, sum2 = len_arr - 1, 0\n",
    "        while j > 0:\n",
    "            sum2 += arr[j]\n",
    "            j -= 1\n",
    "            if sum2 == sum_arr // 3 :\n",
    "                break\n",
    "        print(i,j)\n",
    "        \n",
    "        sum3 = 0\n",
    "        for k in range(i,j+1):\n",
    "            sum3 += arr[k]\n",
    "            k += 1\n",
    "            if sum3 == sum_arr // 3 :\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        ##arr = list(map(int,input().split(',')))\n",
    "        arrSum = sum(arr)\n",
    "        if arrSum % 3 != 0 :\n",
    "            return False\n",
    "        else:\n",
    "            singleSum = arrSum//3\n",
    "            #print(singleSum)\n",
    "            cnt = 3\n",
    "            tempSum = 0\n",
    "            for num in arr:\n",
    "                tempSum += num\n",
    "                if tempSum == singleSum:\n",
    "                    cnt -= 1\n",
    "                    tempSum = 0\n",
    "            if cnt <= 0:\n",
    "                return True\n",
    "            else:\n",
    "                return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        if len(arr) < 3 :\n",
    "            return False\n",
    "        totalSum = 0 \n",
    "        for i in arr:\n",
    "            totalSum += i\n",
    "        if totalSum%3 !=0:\n",
    "            return False\n",
    "        \n",
    "        array1Sum = 0\n",
    "        for i in range(len(arr)-2):\n",
    "            array1Sum += arr[i]\n",
    "            if array1Sum == totalSum/3:\n",
    "                break\n",
    "        \n",
    "        array2Sum = 0\n",
    "        for j in range(i+1,len(arr)-1):\n",
    "            array2Sum += arr[j]\n",
    "            if array2Sum == totalSum/3:\n",
    "                break\n",
    "\n",
    "        array3Sum = totalSum - array1Sum - array2Sum\n",
    "\n",
    "        if (array1Sum == array2Sum & array2Sum==array3Sum):\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        s = sum(arr)\n",
    "        if s % 3 != 0:\n",
    "            return False\n",
    "        s //= 3\n",
    "        cnt = 0\n",
    "        temp_s = 0\n",
    "        for n in arr:\n",
    "            temp_s += n\n",
    "            if temp_s == s:\n",
    "                if cnt == 2:\n",
    "                    return True\n",
    "                cnt += 1\n",
    "                temp_s = 0\n",
    "                \n",
    "        return cnt == 3\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        s = sum(arr)\n",
    "        if s % 3 != 0:\n",
    "            return False\n",
    "        target = s // 3\n",
    "        n,i,cur = len(arr),0,0\n",
    "        while i < n:\n",
    "            cur += arr[i]\n",
    "            if cur == target:\n",
    "                break\n",
    "            i += 1\n",
    "        if cur != target:\n",
    "            return False\n",
    "        j = i + 1\n",
    "        while j + 1 < n:\n",
    "            cur += arr[j]\n",
    "            if cur== target * 2:\n",
    "                return True\n",
    "            j += 1\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, A: List[int]) -> bool:\n",
    "        s = sum(A)\n",
    "        if s % 3 != 0:\n",
    "            return False\n",
    "        target = s // 3\n",
    "        n, i, cur = len(A), 0, 0\n",
    "        while i < n:\n",
    "            cur += A[i]\n",
    "            if cur == target:\n",
    "                break\n",
    "            i += 1\n",
    "        if cur != target:\n",
    "            return False\n",
    "        j = i + 1\n",
    "        while j + 1 < n:  # 需要满足最后一个数组非空\n",
    "            cur += A[j]\n",
    "            if cur == target * 2:\n",
    "                return True\n",
    "            j += 1\n",
    "        return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        if len(arr) < 3 :\n",
    "            return False\n",
    "        totalSum = array1Sum = array2Sum = 0  \n",
    "        for i in arr:\n",
    "            totalSum += i\n",
    "        if totalSum%3 !=0:\n",
    "            return False\n",
    "        \n",
    "       \n",
    "        for i in range(len(arr)-2):\n",
    "            array1Sum += arr[i]\n",
    "            if array1Sum == totalSum/3:\n",
    "                break\n",
    "        \n",
    "\n",
    "        for j in range(i+1,len(arr)-1):\n",
    "            array2Sum += arr[j]\n",
    "            if array2Sum == totalSum/3:\n",
    "                break\n",
    "\n",
    "        array3Sum = totalSum - array1Sum - array2Sum\n",
    "\n",
    "        if (array1Sum == array2Sum & array2Sum==array3Sum):\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\r\n",
    "        arrSum = sum(arr)\r\n",
    "        if arrSum % 3:\r\n",
    "            return False\r\n",
    "        singleSum = arrSum // 3\r\n",
    "        temp = 0\r\n",
    "        cnt = 0\r\n",
    "        for i in arr:\r\n",
    "            temp += i\r\n",
    "            if temp == singleSum:\r\n",
    "                if cnt < 3:\r\n",
    "                    cnt += 1\r\n",
    "                    temp = 0\r\n",
    "        if cnt == 3 and temp == 0:\r\n",
    "            return True\r\n",
    "        else:\r\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        num = sum(arr)\n",
    "        if num%3 != 0:\n",
    "            return False\n",
    "        f, l = 0, len(arr)-1\n",
    "        num_left, num_right = 0, 0\n",
    "        while f < len(arr):\n",
    "            num_left += arr[f]\n",
    "            if num_left == num // 3:\n",
    "                break\n",
    "            f += 1\n",
    "        while l >= 0:\n",
    "            num_right += arr[l]\n",
    "            if num_right == num // 3:\n",
    "                break\n",
    "            l -= 1\n",
    "        return True if l - f > 1 else False\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        l = len(arr)\n",
    "        sum = num = count = 0\n",
    "        for i in range(l):\n",
    "            sum += arr[i]\n",
    "        sum = sum/3\n",
    "        for i in range(l):\n",
    "            num += arr[i]\n",
    "            if num == sum:\n",
    "                count += 1\n",
    "                num = 0\n",
    "                if count < 3:\n",
    "                    continue\n",
    "            if count == 3:\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        s=sum(arr)\n",
    "        if s % 3 != 0:\n",
    "            return False\n",
    "        target = s // 3\n",
    "        n, i, cur = len(arr), 0, 0\n",
    "        while i < n:\n",
    "            cur += arr[i]\n",
    "            if cur == target:\n",
    "                break\n",
    "            i += 1\n",
    "        if cur != target:\n",
    "            return False\n",
    "        j = i + 1\n",
    "        while j + 1 < n:  # 需要满足最后一个数组非空\n",
    "            cur += arr[j]\n",
    "            if cur == target * 2:\n",
    "                return True\n",
    "            j += 1\n",
    "        return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        arr_sum = sum(arr)\n",
    "        if (arr_sum%3) != 0:\n",
    "            return False\n",
    "        l = len(arr)\n",
    "        arr_sum = arr_sum/3\n",
    "        num = count = 0\n",
    "        for i in range(l):\n",
    "            num += arr[i]\n",
    "            if num == arr_sum:\n",
    "                count += 1\n",
    "                num = 0\n",
    "                if count < 3:\n",
    "                    continue\n",
    "            if count == 3:\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        s = sum(arr)\n",
    "        if s % 3 != 0:\n",
    "            return False\n",
    "        target = s // 3\n",
    "        n, i, cur = len(arr), 0, 0\n",
    "        while i < n:\n",
    "            cur += arr[i]\n",
    "            if cur == target:\n",
    "                break\n",
    "            i += 1\n",
    "        if cur != target:\n",
    "            return False\n",
    "        j = i + 1\n",
    "        while j + 1 < n:  # 需要满足最后一个数组非空\n",
    "            cur += arr[j]\n",
    "            if cur == target * 2:\n",
    "                return True\n",
    "            j += 1\n",
    "        return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        _sum = sum(arr)\n",
    "        div, mod = divmod(_sum, 3)\n",
    "        if mod != 0:\n",
    "            return False\n",
    "        result = 0\n",
    "        count = 0\n",
    "        for c in arr:\n",
    "            result += c\n",
    "            if count == 3:\n",
    "                continue\n",
    "            if result == div:\n",
    "                result = 0\n",
    "                count += 1\n",
    "        return result == 0 and count == 3\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        sum_ = sum(arr)\n",
    "        if sum_ % 3 != 0:\n",
    "            return False\n",
    "        singleSum = sum_ // 3\n",
    "        cnt = 3\n",
    "        tempSum = 0\n",
    "        for t in arr:\n",
    "            tempSum += t\n",
    "            if tempSum == singleSum and cnt > 0:\n",
    "                cnt -= 1\n",
    "                tempSum = 0\n",
    "        if cnt == 0 and tempSum == 0:\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        l = len(arr)\n",
    "        sum1 = sum(arr)/3\n",
    "        num = count = 0\n",
    "        for i in range(l):\n",
    "            num += arr[i]\n",
    "            if num == sum1:\n",
    "                count += 1\n",
    "                num = 0\n",
    "                if count < 3:\n",
    "                    continue\n",
    "            if count == 3:\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        n = len(arr)\n",
    "        total = sum(arr)\n",
    "        if total % 3 != 0:\n",
    "            return False\n",
    "        average = total//3\n",
    "        sum_num = 0\n",
    "        cnt = 3\n",
    "        for i in arr:\n",
    "            sum_num += i\n",
    "            if sum_num == average:\n",
    "                cnt -= 1\n",
    "                sum_num = 0\n",
    "            \n",
    "        if cnt > 0:\n",
    "            return False\n",
    "        else:\n",
    "            return True\n",
    "        \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) :\n",
    "        sums = sum(arr)\n",
    "        sum1 = sums/3\n",
    "        if all(arr[i] == 0 for i in range(len(arr))):\n",
    "            return True\n",
    "        total = 0\n",
    "        count = 0\n",
    "        for i in range(len(arr)):\n",
    "                total = total+arr[i]\n",
    "                if total == sum1:\n",
    "                    total = 0\n",
    "                    count = count+1\n",
    "            \n",
    "                \n",
    "            \n",
    "        if count >= 3:\n",
    "                    return True\n",
    "        else:\n",
    "                    return False\n",
    "                    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        if sum(arr)%3:\n",
    "            return False\n",
    "        a = sum(arr) // 3\n",
    "        b = 0\n",
    "        c = 0\n",
    "        for i in arr:\n",
    "            b += i\n",
    "            if b == a:\n",
    "                c += 1\n",
    "                b = 0\n",
    "            if c == 3:\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        s = sum(arr)\n",
    "        if s % 3 != 0:\n",
    "            return False\n",
    "        pref = [0]\n",
    "        for k in arr:\n",
    "            pref.append(pref[-1] + k)\n",
    "        f = 0\n",
    "        for i in range(1, len(pref) - 1):\n",
    "            if pref[i] == s // 3 and f == 0:\n",
    "                f = 1\n",
    "            elif pref[i] == s // 3 * 2 and f == 1:\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        n = len(arr)\n",
    "        arr_sum = sum(arr)\n",
    "        if arr_sum % 3 != 0:\n",
    "            return False\n",
    "        cnt = 0\n",
    "        res = 0\n",
    "        arr_sum = arr_sum // 3\n",
    "        for num in arr:\n",
    "            res += num\n",
    "            if res == arr_sum:\n",
    "                cnt += 1\n",
    "                res = 0\n",
    "                if cnt == 3:\n",
    "                    return True\n",
    "        return False\n",
    "\n",
    "              "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        sum_ = sum(arr)\n",
    "        avg_ = sum_ / 3\n",
    "        if avg_ - int(avg_) > 0: return False\n",
    "        sub_sum = 0\n",
    "        time = 0\n",
    "        for i in range(len(arr)):\n",
    "            sub_sum += arr[i]\n",
    "            if sub_sum == avg_:\n",
    "                sub_sum = 0\n",
    "                time += 1\n",
    "            if time == 2:\n",
    "                if len(arr[i+1:]) > 0 and sum(arr[i+1:]) == avg_:\n",
    "                    return True\n",
    "                else:\n",
    "                    return False\n",
    "\n",
    "        if sub_sum == 0 and time == 3:\n",
    "            return True\n",
    "        else:\n",
    "            return False            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        target = sum(arr)\n",
    "        if target%3 != 0 :\n",
    "            return False\n",
    "        target = target//3\n",
    "        n = len(arr)\n",
    "        cur = 0\n",
    "        count = 0\n",
    "        begin = -1\n",
    "        for i in range(n):\n",
    "            cur += arr[i]\n",
    "            if count<2 and cur==target:\n",
    "                count += 1\n",
    "                cur = 0\n",
    "                if count==2:\n",
    "                    if i+1 != n:\n",
    "                        return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        total_sum = sum(arr)\n",
    "        if total_sum % 3 != 0:\n",
    "            return False\n",
    "        \n",
    "        target_sum = total_sum // 3\n",
    "        count, current_sum = 0, 0\n",
    "\n",
    "        for i, a in enumerate(arr):\n",
    "            current_sum += a\n",
    "            if current_sum == target_sum:\n",
    "                count += 1\n",
    "                current_sum = 0\n",
    "            if count == 2 and i < len(arr) - 1:\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        n = len(arr)\n",
    "        sums = [0] * n\n",
    "        sums[0] = arr[0]\n",
    "        for i in range(1, n):\n",
    "            sums[i] += sums[i - 1] + arr[i]\n",
    "        if sums[-1] % 3 != 0:\n",
    "            return False\n",
    "        num = sums[-1] // 3\n",
    "        for i in range(n - 1):\n",
    "            if sums[i] == num:\n",
    "                for j in range(i + 1, n - 1):\n",
    "                    if sums[j] == num * 2:\n",
    "                        return True\n",
    "                else:\n",
    "                    return False\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        arrSum = sum(arr)\n",
    "        if arrSum % 3 != 0:\n",
    "            return False\n",
    "        singleSum = arrSum // 3\n",
    "        cnt = 3\n",
    "        tempSum = 0\n",
    "        for i in arr:\n",
    "            if tempSum + i == singleSum:\n",
    "                if cnt >0:\n",
    "                    cnt -= 1\n",
    "                    tempSum = 0\n",
    "                else:\n",
    "                    tempSum += i\n",
    "            else:\n",
    "                tempSum += i\n",
    "        if cnt == 0 and tempSum ==0:\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        sum_arr = sum(arr)\n",
    "        if sum_arr % 3 != 0:\n",
    "            return False\n",
    "        s = sum_arr // 3\n",
    "        i, j = 0, len(arr) - 1\n",
    "        s1, s2 = arr[0], arr[-1]\n",
    "        while i < j - 1:\n",
    "            if s1 == s and s2 == s:\n",
    "                return True\n",
    "            if s1 != s:\n",
    "                i += 1\n",
    "                s1 += arr[i]\n",
    "            if s2 != s:\n",
    "                j -= 1\n",
    "                s2 += arr[j]\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        h = sum(arr)\n",
    "        if h % 3 != 0:\n",
    "            return False\n",
    "        else:\n",
    "            n = int(h // 3)\n",
    "            ans = 0\n",
    "            flag = 0\n",
    "            i = 0\n",
    "            while i < len(arr):\n",
    "                ans += arr[i]\n",
    "                i += 1\n",
    "                if ans == n:\n",
    "                    flag += 1\n",
    "                    ans = 0\n",
    "            \n",
    "            return flag >= 3\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        arrSum=sum(arr)\n",
    "        if arrSum%3 != 0:\n",
    "            return False\n",
    "        singleSum=arrSum//3\n",
    "        tempSum=0\n",
    "        cnt=3\n",
    "        for i in arr:\n",
    "            if tempSum+i==singleSum:\n",
    "                if cnt>0:\n",
    "                    cnt-=1\n",
    "                    tempSum=0\n",
    "                else:\n",
    "                    tempSum+=i \n",
    "            else: \n",
    "                tempSum+=i \n",
    "        if cnt==0 and tempSum==0:\n",
    "            return True  \n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        # 先判断数组之和是否能够三等分\n",
    "        numSum = sum(arr)\n",
    "        if (numSum % 3 != 0):\n",
    "            return False\n",
    "        x = numSum // 3\n",
    "        print(x)\n",
    "        pre = 0\n",
    "        cnt = 0\n",
    "        for i in range(len(arr)):\n",
    "            pre += arr[i]\n",
    "            if (pre == x):\n",
    "                pre = 0\n",
    "                cnt += 1\n",
    "        if (cnt >= 3):   # 考虑到一个满足条件的子数组中可能有多个满足条件的子数组，比如数组中0分隔的两个刚好满足条件\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        new_arr=[0]*(len(arr))\n",
    "        s=0\n",
    "        for x in range(len(arr)):\n",
    "            s+=arr[x]\n",
    "            new_arr[x]=s\n",
    "        start=0\n",
    "        while start<len(arr):\n",
    "            if s==3*new_arr[start]:\n",
    "                break\n",
    "            start+=1\n",
    "        else:\n",
    "            return False\n",
    "        remain = new_arr[start+1:len(arr)-1:1]\n",
    "        if 2*new_arr[start] in remain:\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\r\n",
    "        aarr = [0] * (len(arr)+1)\r\n",
    "        sa = sum(arr)\r\n",
    "        if sa % 3:\r\n",
    "            return False\r\n",
    "        flag = 0\r\n",
    "        for i in range(len(arr)):\r\n",
    "            aarr[i+1] = aarr[i] + arr[i]\r\n",
    "            if flag ==0 and aarr[i+1] == sa//3:\r\n",
    "                flag = 1\r\n",
    "            elif flag == 1 and aarr[i+1] == sa//3*2:\r\n",
    "                flag = 2\r\n",
    "            elif flag ==2 and aarr[i+1] == sa:\r\n",
    "                return True\r\n",
    "        return False\r\n",
    "\r\n",
    "\r\n",
    "\r\n",
    "\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        if sum(arr) % 3 != 0:\n",
    "            return False\n",
    "        ss = sum(arr) // 3\n",
    "        count = 0\n",
    "        sum_ = 0\n",
    "        for x in arr:\n",
    "            sum_ += x\n",
    "            if sum_ == ss:\n",
    "                count += 1\n",
    "                sum_ = 0\n",
    "        return count >= 3\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        s = sum(arr)\n",
    "        if s % 3 != 0:\n",
    "            return False\n",
    "        target = s // 3\n",
    "        n, i, cur = len(arr), 0, 0\n",
    "        while i < n:\n",
    "            cur += arr[i]\n",
    "            if cur == target:\n",
    "                break\n",
    "            i += 1\n",
    "        if cur != target:\n",
    "            return False\n",
    "        j = i + 1\n",
    "        while j + 1 < n:\n",
    "            cur += arr[j]\n",
    "            if cur == target * 2:\n",
    "                return True\n",
    "            j += 1\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        tar=int(sum(arr)/3)\n",
    "        n=len(arr)\n",
    "        p=0\n",
    "        p1=n\n",
    "        p2=n\n",
    "        for i in range(n):\n",
    "            p=arr[i]+p\n",
    "            if p == tar:\n",
    "                if p1 == n:\n",
    "                    p1=i\n",
    "                elif p2 == n:\n",
    "                    p2=i\n",
    "                    break\n",
    "                p=0\n",
    "        if p1 == n-1 or p2 == n-1:\n",
    "            return False\n",
    "        elif sum(arr[:p1+1]) == sum(arr[p1+1:p2+1]) == sum(arr[p2+1:]):\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        s = sum(arr)\n",
    "        if s % 3: return False\n",
    "        target = s // 3\n",
    "        i = 0\n",
    "        s = 0\n",
    "        while i < len(arr):\n",
    "            s += arr[i]\n",
    "            if s == target:\n",
    "                break\n",
    "            i += 1\n",
    "        s, j = 0, len(arr) - 1\n",
    "        while j >= 0:\n",
    "            s += arr[j]\n",
    "            if s == target:\n",
    "                break\n",
    "            j -= 1\n",
    "        return i + 1 < j "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, A: List[int]) -> bool:\n",
    "        s=sum(A)\n",
    "        if s%3!=0:\n",
    "            return False\n",
    "        target=s//3\n",
    "        n=len(A)\n",
    "        i,cou=0,0\n",
    "        while i<n:\n",
    "            cou+=A[i]\n",
    "            if cou==target:\n",
    "                break\n",
    "            i+=1\n",
    "        if cou!=target:\n",
    "            return False\n",
    "        j=i+1\n",
    "        while j+1<n:\n",
    "            cou+=A[j]\n",
    "            if cou==target*2:\n",
    "                return True\n",
    "            j+=1\n",
    "        return False\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        Sum = sum(arr)\n",
    "        if Sum%3 != 0:\n",
    "            return False\n",
    "        Sum = int(Sum/3)\n",
    "        index1, index2, n = 0,0,len(arr)\n",
    "        tmp = 0\n",
    "        while index1 < n:\n",
    "            tmp += arr[index1]\n",
    "            if tmp == Sum:\n",
    "                break\n",
    "            index1 += 1\n",
    "        if index1 >= n-1:\n",
    "            return False\n",
    "        index2 = index1+1\n",
    "        tmp = 0\n",
    "        while index2 < n:\n",
    "            tmp += arr[index2]\n",
    "            if tmp == Sum:\n",
    "                break\n",
    "            index2 += 1\n",
    "        if index2 >= n-1:\n",
    "            return False\n",
    "        if sum(arr[index2+1:]) == Sum:\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        sumarr = sum(arr)\n",
    "        if sumarr % 3 != 0:\n",
    "            return False\n",
    "        k = sumarr // 3\n",
    "        print(k)\n",
    "        suminterval = 0\n",
    "        cnt = 0\n",
    "        for i in arr:\n",
    "            if suminterval + i == k:\n",
    "                if cnt != 3:\n",
    "                    cnt += 1\n",
    "                    suminterval = 0\n",
    "                else: suminterval += i\n",
    "            else: suminterval += i\n",
    "        \n",
    "        if cnt == 3 and suminterval == 0:\n",
    "            return True \n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        if len(arr) < 3 :\n",
    "            return False\n",
    "        totalSum = array1Sum = array2Sum = 0  \n",
    "        for i in arr:\n",
    "            totalSum += i\n",
    "        if totalSum%3 !=0:\n",
    "            return False\n",
    "        \n",
    "       \n",
    "        for i in range(len(arr)-2):\n",
    "            array1Sum += arr[i]\n",
    "            if array1Sum == totalSum/3:\n",
    "                break\n",
    "        \n",
    "\n",
    "        for j in range(i+1,len(arr)-1):\n",
    "            array2Sum += arr[j]\n",
    "            if array2Sum == totalSum/3:\n",
    "                break\n",
    "\n",
    "        array3Sum = totalSum - array1Sum - array2Sum\n",
    "\n",
    "        if (array1Sum == array2Sum & array2Sum==array3Sum):\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        if sum(arr) % 3 != 0:\n",
    "            return False\n",
    "\n",
    "        \n",
    "        target = sum(arr)//3 \n",
    "        divs = self.allDivisions(0, arr, target)\n",
    "        return 3 in set(divs)\n",
    "        \n",
    "\n",
    "    def allDivisions(self, start, arr, target):\n",
    "        s = 0\n",
    "        it = 0\n",
    "        result = []\n",
    "        for i in range(start, len(arr)):\n",
    "            s += arr[i]\n",
    "            if s == target:\n",
    "                s = 0\n",
    "                it += 1\n",
    "                result += self.allDivisions(i + 1, arr, target)\n",
    "        result.append(it)\n",
    "        # print(result)\n",
    "\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        arrSum=sum(arr)   #先算出数组总和\n",
    "        if arrSum%3 != 0:  #如果输入能被3整除，则能分为3部分，否则，不能\n",
    "            return False\n",
    "        singleSum=arrSum//3  #算出每部分的和\n",
    "        tempSum=0   #初始化和为0\n",
    "        cnt=3   #总共需要算三次和\n",
    "        for i in arr:  \n",
    "            if tempSum+i==singleSum:\n",
    "                if cnt>0:  #如果还有批次需要算和，才需要初始化\n",
    "                    cnt-=1\n",
    "                    tempSum=0\n",
    "                else:\n",
    "                    tempSum+=i \n",
    "            else: \n",
    "                tempSum+=i \n",
    "        if cnt==0 and tempSum==0:\n",
    "            return True  \n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        avg = sum(arr)/3\n",
    "        if avg%1 ==0:\n",
    "            s = 0\n",
    "            l = []\n",
    "            for i in range(len(arr)):\n",
    "                s += arr[i]\n",
    "                if s == avg:\n",
    "                    l.append(s)\n",
    "                    s = 0\n",
    "                    if len(l) == 2 and i != len(arr) -1:\n",
    "                        c = sum(arr[i+1:])\n",
    "                        l.append(c)\n",
    "                        return l[1] == l[0] == l[2]\n",
    "        return False\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, A: List[int]) -> bool:\n",
    "        s = sum(A)\n",
    "        if s % 3 != 0:\n",
    "            return False\n",
    "        target = s // 3\n",
    "        n, i, cur = len(A), 0, 0\n",
    "        while i < n:\n",
    "            cur += A[i]\n",
    "            if cur == target:\n",
    "                break\n",
    "            i += 1\n",
    "        if cur != target:\n",
    "            return False\n",
    "        j = i + 1\n",
    "        while j + 1 < n:  # 需要满足最后一个数组非空\n",
    "            cur += A[j]\n",
    "            if cur == target * 2:\n",
    "                return True\n",
    "            j += 1\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        sum_ = sum(arr)\n",
    "        avg_ = sum_ / 3\n",
    "        if avg_ - int(avg_) > 0: return False\n",
    "        sub_sum = 0\n",
    "        time = 0\n",
    "        for i in range(len(arr)):\n",
    "            sub_sum += arr[i]\n",
    "            if sub_sum == avg_:\n",
    "                sub_sum = 0\n",
    "                time += 1\n",
    "            if time == 2:\n",
    "                if len(arr[i+1:]) > 0 and sum(arr[i+1:]) == avg_:\n",
    "                    return True\n",
    "                else:\n",
    "                    return False\n",
    "\n",
    "        if sub_sum == 0 and time == 3:\n",
    "            return True\n",
    "        else:\n",
    "            return False            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# python3模拟\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        asum = sum(arr)\n",
    "        if asum % 3:\n",
    "            return False\n",
    "        alen = len(arr)\n",
    "        ref = asum // 3\n",
    "        t,res = 0,0\n",
    "        for i in range(alen):\n",
    "            t += arr[i]\n",
    "            if t == ref:\n",
    "                res += 1\n",
    "                t = 0\n",
    "                if res == 2 and i < alen-1:\n",
    "                    return True\n",
    "        return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        和 = sum(arr)\n",
    "        if 和%3:\n",
    "            return False\n",
    "        值 = 和//3\n",
    "        标记 = False\n",
    "        计数 = 0\n",
    "        for i in arr[:-1]:\n",
    "            计数 += i\n",
    "            if 计数 == 值:\n",
    "                if 标记:\n",
    "                    return True\n",
    "                值 *= 2\n",
    "                标记 = True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        new_arr=[0]*(len(arr))\n",
    "        s=0\n",
    "        for x in range(len(arr)):\n",
    "            s+=arr[x]\n",
    "            new_arr[x]=s\n",
    "        start=0\n",
    "        while start<len(arr):\n",
    "            if s==3*new_arr[start]:\n",
    "                break\n",
    "            start+=1\n",
    "        else:\n",
    "            return False\n",
    "        remain = new_arr[start+1:len(arr)-1:1]\n",
    "        if 2*new_arr[start] in remain:\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        p=sum(arr)\n",
    "        if p%3!=0:\n",
    "            return False\n",
    "        div=p//3\n",
    "        i=0\n",
    "        temp=0\n",
    "        count=0\n",
    "        while i<len(arr)-1:\n",
    "            temp+=arr[i]\n",
    "            if temp==div:\n",
    "                temp=0\n",
    "                count+=1\n",
    "                if count==2:\n",
    "                    return True\n",
    "            i+=1\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def canThreePartsEqualSum(self, arr: list[int]) -> bool:\r\n",
    "        s = [0]\r\n",
    "        for n in arr:\r\n",
    "            s.append(s[-1] + n)\r\n",
    "\r\n",
    "        if s[-1] % 3 != 0:\r\n",
    "            return False\r\n",
    "        target = s[-1] // 3\r\n",
    "        b_target = 2 *target\r\n",
    "        front  = False\r\n",
    "        size = len(arr)\r\n",
    "        for i in range(size):\r\n",
    "            if not front:\r\n",
    "                if s[i + 1] == target:\r\n",
    "                    front = True\r\n",
    "            else:\r\n",
    "                if s[i + 1] == b_target:\r\n",
    "                    if i != size - 1:\r\n",
    "                        return True\r\n",
    "                    return False\r\n",
    "        return False\r\n",
    "\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        sum_num=sum(arr)\n",
    "        if sum_num%3!=0:\n",
    "            return False\n",
    "        idt_1=0\n",
    "        idt_2=0\n",
    "\n",
    "        num=None\n",
    "\n",
    "        while idt_1<len(arr) and num!=sum_num//3:\n",
    "            if num==None:\n",
    "                num=arr[idt_1]\n",
    "            else:\n",
    "                num+=arr[idt_1]\n",
    "            idt_1+=1\n",
    "        print(idt_1,idt_2)\n",
    "        if idt_1>=len(arr):\n",
    "            return False\n",
    "\n",
    "        idt_2=idt_1\n",
    "        num=None\n",
    "        while idt_2<len(arr) and num!=sum_num//3:\n",
    "            if num==None:\n",
    "                num=arr[idt_2]\n",
    "            else:\n",
    "                num+=arr[idt_2]\n",
    "            idt_2+=1\n",
    "        print(idt_1,idt_2)\n",
    "        if idt_2>=len(arr):\n",
    "            return False\n",
    "\n",
    "        if sum(arr[idt_2:])!=sum_num//3 or not arr[idt_2:]:\n",
    "            print(idt_1,idt_2)\n",
    "            return False\n",
    "        \n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        if sum(arr) % 3:\n",
    "            return False\n",
    "        n = sum(arr) // 3\n",
    "        for i in range(1,len(arr)):\n",
    "            if sum(arr[:i]) == n:\n",
    "                for j in range(len(arr)-1,i,-1):\n",
    "                    if sum(arr[j:]) == n:\n",
    "                        return sum(arr[i:j]) == n and i != j\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        total_sum = sum(arr)  # 计算整个数组的总和\n",
    "        if total_sum % 3 != 0:  # 如果总和不是3的倍数，则直接返回False\n",
    "            return False\n",
    "\n",
    "        target = total_sum // 3  # 计算每个部分的目标和\n",
    "        n = len(arr)\n",
    "        s = 0\n",
    "        count = 0  # 计数器来跟踪已经找到了多少部分和等于目标和的部分\n",
    "\n",
    "        for i in range(n):\n",
    "            s += arr[i]  # 累加当前元素的值\n",
    "            if s == target:  # 如果累加的和达到了目标和\n",
    "                count += 1\n",
    "                s = 0  # 重置累加和以检查下一个部分\n",
    "\n",
    "        # 如果可以找到三个和相等的部分，则count应该为3\n",
    "        return count >= 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        amount=sum(arr)\n",
    "        div=amount/3\n",
    "        off=amount%3\n",
    "        if off!=0:\n",
    "            return False\n",
    "        else:\n",
    "            res=[]\n",
    "            total=0\n",
    "            for i in range(len(arr)):\n",
    "                total+=arr[i]\n",
    "                if total == div:\n",
    "                    res.append(div)\n",
    "                    total=0\n",
    "\n",
    "                    if len(res)==2 and i<len(arr)-1:\n",
    "                        total =sum(arr[i+1:])\n",
    "                        if total!=div:\n",
    "                            return False\n",
    "                        else:\n",
    "                            return True\n",
    "            return False                \n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        all_sum = sum(arr)\n",
    "        if all_sum % 3 != 0:\n",
    "            return False\n",
    "        single_sum = all_sum // 3\n",
    "        count = 0\n",
    "        selected = 0\n",
    "        print(single_sum)\n",
    "        for index in range(len(arr)):\n",
    "            count += arr[index]\n",
    "            if count == single_sum:\n",
    "                selected += 1\n",
    "                count = 0\n",
    "            if (selected == 2 and sum(arr[index+1:]) == single_sum and index < len(arr)-1):\n",
    "                selected += 1\n",
    "                break\n",
    "                \n",
    "\n",
    "        return selected == 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        avg, left = divmod(sum(arr), 3)\n",
    "        if left > 0:\n",
    "            return False\n",
    "        n = len(arr)\n",
    "        total = cnt = 0\n",
    "        for i in range(n):\n",
    "            total += arr[i]\n",
    "            if total == avg:\n",
    "                if cnt < 2 or cnt == 2 and i == n - 1:\n",
    "                    total = 0\n",
    "                    cnt += 1\n",
    "        return cnt == 3 and total == 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        new_arr=[0]*(len(arr))\n",
    "        s=0\n",
    "        for x in range(len(arr)):\n",
    "            s+=arr[x]\n",
    "            new_arr[x]=s\n",
    "        res_s=-1\n",
    "        start=0\n",
    "        while start<len(arr):\n",
    "            if s==3*new_arr[start]:\n",
    "                res_s=start\n",
    "                break\n",
    "            start+=1\n",
    "        else:\n",
    "            return False\n",
    "        mid = start+1\n",
    "        remain=new_arr[mid:len(arr)-1:1]\n",
    "        if 2*new_arr[start] in remain:\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        sTotal = sum(arr)\n",
    "        if sTotal % 3 != 0:\n",
    "            return False\n",
    "        \n",
    "        sPart = sTotal // 3        \n",
    "        length = len(arr)\n",
    "        print(\"sPart, length: \", sPart, length)\n",
    "\n",
    "        s1 = arr[0]\n",
    "        i = 1\n",
    "        while i < length-2 and s1 != sPart:\n",
    "            s1 += arr[i]\n",
    "            i += 1\n",
    "        print(\"i: \", i)\n",
    "\n",
    "        if i > length-2:\n",
    "            return False\n",
    "                \n",
    "        \n",
    "        s2 = arr[i]\n",
    "        j = i+1\n",
    "        while j < length and s2 != sPart:\n",
    "            s2 += arr[j]\n",
    "            j += 1\n",
    "        print(\"j: \", j)\n",
    "\n",
    "        if j > length-1:\n",
    "            return False\n",
    "        \n",
    "\n",
    "        return True\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        if sum(arr) % 3 != 0:\n",
    "            return False\n",
    "        avg = sum(arr) // 3\n",
    "        print(avg)\n",
    "        left, right = 0, len(arr) - 1\n",
    "        left_sum, right_sum = arr[0], arr[-1]\n",
    "        while left < right - 1:\n",
    "            if left_sum == avg and right_sum == avg:\n",
    "                return True\n",
    "            if right_sum != avg:\n",
    "                right -= 1\n",
    "                right_sum += arr[right]\n",
    "            if left_sum != avg:\n",
    "                left += 1\n",
    "                left_sum += arr[left]\n",
    "        return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        s = sum(arr)\n",
    "        for i in range(len(arr)):\n",
    "            if sum(arr[:i+1]) == s/3:\n",
    "                for j in range(i+2,len(arr)):\n",
    "                    if sum(arr[i+1:j]) == s/3 and sum(arr[j:]) == s/3:\n",
    "                        return True\n",
    "                break\n",
    "        return False\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        n = len(arr)\n",
    "        total = sum(arr)\n",
    "        if total % 3 != 0:\n",
    "            return False\n",
    "        i = 1\n",
    "        while i <= n-2:\n",
    "            if sum(arr[:i]) == total / 3:\n",
    "                break\n",
    "            i += 1\n",
    "        if i == n-1:\n",
    "            return False\n",
    "        j = i + 1\n",
    "        while j <= n-1:\n",
    "            if sum(arr[:j]) == total / 3 * 2:\n",
    "                break\n",
    "            j += 1\n",
    "        if j == n:\n",
    "            return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canThreePartsEqualSum(self, arr: List[int]) -> bool:\n",
    "        all_sum = sum(arr)\n",
    "        if all_sum % 3 != 0:\n",
    "            return False\n",
    "        single_sum = all_sum // 3\n",
    "        count = 0\n",
    "        selected = 0\n",
    "        print(single_sum)\n",
    "        for index in range(len(arr)):\n",
    "            count += arr[index]\n",
    "            if count == single_sum:\n",
    "                selected += 1\n",
    "                count = 0\n",
    "            if (selected == 2 and sum(arr[index+1:]) == single_sum and index < len(arr)-1):\n",
    "                selected += 1\n",
    "                break\n",
    "                \n",
    "\n",
    "        return selected == 3"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
